﻿using MathNet.Numerics;
using SeeSharpTools.JY.DSP.Utility.Filter1D;
using Sunny.UI;
using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace SeeSharpTools.Demo
{
    public partial class FormDecimate : UIPage
    {
        public FormDecimate()
        {
            InitializeComponent();

            uiComboBox_AntiFilterType.Items.AddRange(Enum.GetNames(typeof(AntialiasingFilterType)));
            uiComboBox_AntiFilterType.SelectedIndex = 0;
        }

        private void uiButton_Start_Click(object sender, EventArgs e)
        {
            AntialiasingFilterType filterType = (AntialiasingFilterType)Enum.Parse(typeof(AntialiasingFilterType), uiComboBox_AntiFilterType.Text);
            double[] t, x, y;
            double[][] xData = new double[2][];
            double[][] yData = new double[2][];
            int r = uiIntegerUpDown_Factor.Value;

            try
            {
                if (filterType == AntialiasingFilterType.IIR)
                {
                    // Create a sinusoidal signal sampled at 4 kHz. Decimate it by a factor of four
                    t = Generate.LinearRange(0, 1.0 / 4000, 1);
                    x = t.Select(num => Math.Sin(2 * Math.PI * 30 * num) + Math.Sin(2 * Math.PI * 60 * num)).ToArray();
                    y = Multirate.Decimate(x, r, filterType);

                    xData[0] = Generate.LinearRange(0, 1, 120);
                    xData[1] = Generate.LinearRange(0, r, 120);
                    yData[0] = x.Take(121).ToArray();
                    yData[1] = y.Take(31).ToArray();
                }
                else
                {
                    // Create a signal with two sinusoids. Decimate it by a factor of 13 using an FIR filter of order 82
                    t = Generate.LinearRange(16, 365);
                    x = t.Select(num => Math.Sin(2 * Math.PI * num / 153) + Math.Cos(2 * Math.PI * num / 127)).ToArray();
                    y = Multirate.Decimate(x, r, filterType, 82);

                    xData[0] = Generate.LinearRange(0, 1, x.Length - 1);
                    xData[1] = Generate.LinearRange(0, r, x.Length - 1);
                    yData[0] = x;
                    yData[1] = y;
                }
                easyChartX1.Plot(xData, yData);
                easyChartX1.Series[0].Type = SeeSharpTools.JY.GUI.EasyChartXSeries.LineType.Bar;
                easyChartX1.Series[0].Name = "原始信号";
                easyChartX1.Series[1].Type = SeeSharpTools.JY.GUI.EasyChartXSeries.LineType.Point;
                easyChartX1.Series[1].Name = "抽取信号";
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void uiComboBox_AntiFilterType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (uiComboBox_AntiFilterType.SelectedIndex == 0)
            {
                uiIntegerUpDown_Factor.Value = 4;
            }
            else
            {
                uiIntegerUpDown_Factor.Value = 13;
            }
        }
    }
}